<template>
  <el-dialog title="重置密码" :visible.sync="isShow" width="800px" destroy-on-close>
    <el-form ref="form" :model="password" :rules="rules" label-width="80px">
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="password.newPassword" placeholder="请输入新密码" type="password" show-password/>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="password.confirmPassword" placeholder="请确认密码" type="password" show-password/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
// import { changePassword } from '@/api/login'
// import { resetPwdForAdmin } from "@/api/system/user.js";
import { request_post } from '@/api/common.js'
import { getToken, removeToken} from '@/utils/auth'
import md5 from 'js-md5';
export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      var pwdRegex = new RegExp('(?=.*[0-9])(?=.*[a-z]).{6,50}$')
      if (!pwdRegex.test(value)) {
      // if (!value || value.length < 6) {
        callback(new Error('密码6-50位，且必须包含字母、数字'))
      } else {
        callback()
      }
    };
    const equalToPassword = (rule, value, callback) => {
      if (this.password.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      test: "1test",
      currentDialog: '',
      isShow: false,
      readonly:true,
      itemModel: {
      },
      password:{
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { min: 6, max: 50, message: "长度在 6 到 50 个字符", trigger: "blur" },
          { validator: validatePassword, trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    handlerIptClick(){
      this.readonly = false
    },
    show(e, currentDialog){
      this.isShow = true
      this.currentDialog = currentDialog
      this.itemModel = e
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          request_post('/user/resetPwdForAdmin/', {
            "userId": this.itemModel.id,
            "password": md5(this.password.newPassword)
          }).then((res) => {
            if(res.data.code == 10000) {
              this.isShow = false
              this.$emit("confirm")
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            } else {
              this.msgError("修改失败");
            }
              
            }
          );
        }
      });
    }
  }
};
</script>
